<template>
  <div id="login-or-register">
    <nav-bar class="nav-bar">
      <div slot="left" class="back" @click="backClick">
        <img src="~assets/img/common/back.svg" alt="" />
      </div>
      <div slot="center">欢迎使用文刀仙游</div>
    </nav-bar>
    <div class="input">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="70px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username" class="inputt" @blur="nameBlur"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            class="inputt"
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            class="inputt"
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input class="inputt" v-model.number="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitRegister('ruleForm')"
            >注册</el-button
          >
          <el-button type="primary" @click="submitLogin('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
import { login, register, judge} from "network/profile";
export default {
  name: "LoginOrRegister",
  data() {
    var checkUsername = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("用户名不能为空"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        pass: "",
        checkPass: "",
        phone: "",
      },
      rules: {
        username: [{ validator: checkUsername, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        phone: [{ validator: checkPhone, trigger: "blur" }],
      },
    };
  },
  components: {
    NavBar,
  },
  methods: {
    backClick() {
      this.$router.push("/profile");
    },
    nameBlur() {
      if(this.ruleForm.username !== "") {
        this._judge(this.ruleForm.username);
      }
    },
    submitRegister(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this._register(this.ruleForm);
        } else {
          this.$toast.show("请正确填写注册信息", 2000);
          return false;
        }
      });
    },
    submitLogin(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this._login(this.ruleForm);
        } else {
          this.$toast.show("请正确填写登录信息", 2000);
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    _register(obj) {
      register(obj).then((res) => {
        console.info(res);
        if (res.data === "Yes") {
          this.$toast.show("注册成功", 2000);
        } else {
          this.$toast.show("注册失败", 2000);
        }
      });
    },
    _login(obj) {
      login(obj).then((res) => {
        console.info(res);
        if (res.data !== null) {
          console.info(res.data);
          this.$store.dispatch("addUser", res.data).then((res) => {
            this.$toast.show(res, 2000);
            this.$router.push("/profile");
          });
        } else {
          this.$toast.show("登录失败", 2000);
        }
      });
    },
    _judge(username) {
      judge(username).then((res) => {
        if(res.data === "Yes") {
          this.$toast.show("用户名可用")
        }else {
          this.$toast.show("用户名不可用")
        }
      })
    },

  },
};
</script>

<style scoped>
#login-or-register {
  /* 可以进一步调整 */
  /* background-image: url(~assets/img/common/placeholder.png); */
  background-position: 50% 50%;
}
.nav-bar {
  position: relative;
  z-index: 9;
  background-color: rgb(153, 255, 0);
  color: #fff;
}
.but {
  margin-top: 50px;
  text-align: center;
}
.input {
  margin-top: 250px;
}
.inputt {
  background: rgba(255，255，255，0);
  opacity: 0.5;
}
</style>